﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelReport
{
    // graphs the devices' utilization
    class DeviceReport
    {
        object misValue;
        Excel.Application excelApp;
        Excel.Workbook workbook;

        public DeviceReport()
        {
            misValue = System.Reflection.Missing.Value;
            excelApp = new Excel.Application();
            // Make the object visible.
            excelApp.Visible = true;
            workbook = excelApp.Workbooks.Add(1);
        }


        /* file will be:
         * time: 
         * SSD: ssd util
         * FC:  fc util
         * SATA: sata util
        */
        public void DeviceUtilization(int iter)
        {
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = "Utilization"+iter.ToString();
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 400, 250);
            Excel.Chart chartPage = myChart.Chart;
            var seriesCol = chartPage.SeriesCollection(misValue);
            chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
            //reading from file
            using (StreamReader sr = new StreamReader("../../../DataBase/OutputFiles/DeviceUtilizationReport" + iter.ToString() + ".txt"))
            {
                String line;
                char[] delimiters = new char[] { '\t' };
                line=sr.ReadLine();
                string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                int count = Convert.ToInt32(line), index=0;
                double[] SSDutil = new double[count];
                double[] FCutil = new double[count];
                double[] SATAutil = new double[count];
                int[] times = new int[count];
                while ((line = sr.ReadLine())!=null){
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    times[index] = Convert.ToInt32(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    SSDutil[index] = Convert.ToDouble(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    FCutil[index] = Convert.ToDouble(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    SATAutil[index] = Convert.ToDouble(parts[1]);
                    index++;
                }
                int series = 1;
                string col;
                worksheet.Cells[1, 1] = "Times";
                worksheet.Cells[2, 1] = "SSD";
                worksheet.Cells[3, 1] = "FC";
                worksheet.Cells[4, 1] = "SATA";
                for (int i = 0; i < count; i++)
                {
                    worksheet.Cells[1, i+2] = times[i];
                    worksheet.Cells[2, i+2] = SSDutil[i];
                    worksheet.Cells[3, i+2] = FCutil[i];
                    worksheet.Cells[4, i+2] = SATAutil[i];
                }

                Excel.Range chartRange1, chartRange2;
                char cColumn = 'A';
                for (int i = 0; i < count; i++)
                    cColumn++;
                col = cColumn.ToString();
                chartRange2 = worksheet.Range["B1", col + "1"];
                chartRange1 = worksheet.Range["B2", col + "2"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "SSD";
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B3", col + "3"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "FC";
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B4", col + "4"];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "SATA";
                seriesCol(series).XValues = chartRange2;
            }
        }

        public void DivisionPriorityGroup(int iter, String group)
        {
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = group + "Group" + iter.ToString();
            char colChar = 'A';
            //reading from file
            using (StreamReader sr = new StreamReader("../../../DataBase/OutputFiles/Division" + group + "GroupReport" + iter.ToString() + ".txt"))
            {
                String line;
                char[] delimiters = new char[] { '\t' };
                line = sr.ReadLine();
                string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                int count = Convert.ToInt32(line), index = 0;
                double[] SSDutil = new double[count];
                double[] FCutil = new double[count];
                double[] SATAutil = new double[count];
                int[] times = new int[count];
                while ((line = sr.ReadLine()) != null)
                {
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    times[index] = Convert.ToInt32(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    SSDutil[index] = Convert.ToDouble(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    FCutil[index] = Convert.ToDouble(parts[1]);
                    line = sr.ReadLine();
                    parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    SATAutil[index] = Convert.ToDouble(parts[1]);
                    index++;
                }
                worksheet.Cells[1, 1] = "Times";
                worksheet.Cells[2, 1] = "SSD";
                worksheet.Cells[3, 1] = "FC";
                worksheet.Cells[4, 1] = "SATA";
                for (int i = 0; i < count; i++)
                {
                    worksheet.Cells[1, i + 2] = times[i];
                    worksheet.Cells[2, i + 2] = SSDutil[i];
                    worksheet.Cells[3, i + 2] = FCutil[i];
                    worksheet.Cells[4, i + 2] = SATAutil[i];
                    colChar++;
                }
            }
            
            Excel.Range dataRange = worksheet.get_Range("B1", colChar+"4");
            Excel.ChartObjects chartObjects = (Excel.ChartObjects)(worksheet.ChartObjects(misValue));
            Excel.ChartObject newChartObject = chartObjects.Add(10, 80, 450, 300);
            newChartObject.Chart.ChartWizard(dataRange, Excel.XlChartType.xlColumnClustered, 1, Excel.XlRowCol.xlRows, 1, 0, false, "", "Time", "Percentage", misValue);

            dump(worksheet);

        }
        private void dump(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine(ex);
                Environment.Exit(-1);
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}
